//
//  KZLoginOpterationView.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/23.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

import RxSwift
import RxCocoa


class KZLoginOperationView: UIView {
    
    let bag = DisposeBag()
    let codeTime = BehaviorRelay(value: 0)
    

    // MARK: ------ property
    // data
    private var inputTexts: [String] = []
    
    
    var inputValues: [String] {
        var values = [String]()
        for tag in 100...100 + inputTexts.count - 1 {
            values.append((bgView.viewWithTag(tag)!.viewWithTag(22) as! UITextField).text!)
        }
        return values
    }
    
    
    // view
    let bgView = UIView()
    
    // MARK: ------ init
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    convenience init(inputTexts: [String]) {
        self.init(frame: .zero)
        self.inputTexts = inputTexts
        
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: ------ 创建视图
    func setupUI() {
        
        self.layer.shadowOpacity = 0.5
        self.layer.shadowOffset = CGSize(width: 0, height: 3)
        self.layer.shadowColor = kThemeColor.cgColor
        
        // 创建背景视图-圆角
        self.addSubview(bgView)
        bgView.layer.cornerRadius = 4
        bgView.backgroundColor = .white
        bgView.snp.makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        
        
        // 循环创建输入框
        var last: UIView? = nil
        for (i, text) in inputTexts.enumerated() {
            
            let view = UIView()
            view.tag = 100 + inputTexts.index(of: text)!
            bgView.addSubview(view)
            view.snp.makeConstraints({ (make) in
                if let last = last {
                    make.top.equalTo(last.snp.bottom).offset(20)
                } else {
                    make.top.equalTo(50)
                }
                make.left.equalTo(36)
                make.right.equalTo(-36)
                make.height.equalTo(30)
            })
            
            let tf = UITextField()
            tf.tag = 22
            tf.placeholder = text
            tf.textColor = .black
            tf.font = kFont(15)
            tf.textAlignment = .left
            view.addSubview(tf)
            tf.snp.makeConstraints({ (make) in
                make.left.equalTo(0)
                make.top.bottom.right.equalTo(view)
            })
            
            let line = UIView()
            line.backgroundColor = kThemeColor
            view.addSubview(line)
            line.snp.makeConstraints({ (make) in
                make.left.right.bottom.equalTo(view)
                make.height.equalTo(1)
            })
            
            if i == 0 {
                
                let codeButton = UIButton(type: .custom)
                codeButton.titleLabel?.font = kFont(14)
                codeButton.setTitleColor(kThemeColor, for: .normal)
                codeButton.setTitleColor(UIColor.lightGray, for: .disabled)
                codeButton.addTarget(self, action: #selector(onSendCode(button:)), for: .touchUpInside)
                view.addSubview(codeButton)
                codeButton.snp.makeConstraints { (m) in
                    m.top.bottom.right.equalTo(0)

                }
            
                // 控制按钮的文字
                codeTime.map({
                    if $0 <= 0 {
                        return "发送验证码"
                    } else {
                        return "再次发送\($0)s"
                    }
                }).bind(to: codeButton.rx.title()).disposed(by: bag)
                
                // 控制
                codeTime.map({
                    return $0 <= 0
                }).bind(to: codeButton.rx.isEnabled).disposed(by: bag)
                
                // 点击
                codeButton.rx.tap.bind {
                    
                    self.codeTime.accept(5)
                    
                    let interval = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
                    interval.takeWhile({
                        return $0 < 5
                    }).subscribe(onNext: { _ in
                        self.codeTime.accept(self.codeTime.value - 1)
                    }).disposed(by: self.bag)
                    
                }.disposed(by: bag)
                
                
            }
            
            last = view
        }
        
        last!.snp.makeConstraints { (m) in
            m.bottom.equalTo(-40)
        }
        
        
    }
    
    
    
}

// MARK: ------ 事件
extension KZLoginOperationView {
    
    
    @objc func onSendCode(button: UIButton) {

    }
}
